package com.pinapel.aixml.controller;

import java.awt.Component;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import com.pinapel.aixml.controller.Editor.AbstractTextEditorController;
import com.pinapel.aixml.controller.Editor.IGraphEditorController;
import com.pinapel.aixml.controller.Editor.SchemaTextController;
import com.pinapel.aixml.controller.Editor.XMLTextController;
import com.pinapel.aixml.controller.Editor.XSDTextController;
import com.pinapel.aixml.graphedit.MainGraphEdition;
import com.pinapel.aixml.models.EnumModelType;
import com.pinapel.aixml.models.Model;
import com.pinapel.aixml.models.xml.DomModel;
import com.pinapel.aixml.models.xml.DomNode;
import com.pinapel.aixml.ui.EditorsUI;
import com.pinapel.aixml.ui.editors.EditorGraphMode;
import com.pinapel.aixml.ui.editors.EditorSemanticMode;
import com.pinapel.aixml.ui.editors.EditorSyntaxMode;
import com.pinapel.aixml.ui.editors.EditorTextMode;


public class EditorsController implements IEditorsController{

	private IMainController mMainController;

	/*
	private ITextEditorController mTextC;
	private IGraphEditorController mGraphC;
	private ISemanticController mSemC;
	private ISyntaxController mSyntaxC;
	*/
	
	private XMLTextController mTextC;
	private IGraphEditorController mGraphC;
	private SchemaTextController mSemC;
	private XSDTextController mSyntaxC;
	
	private EditorsUI mView;
	
	public EditorsController(IMainController topController) {
		mMainController = topController;
		
//		mTextC = new TextEditorController(this);
		
		mTextC = new XMLTextController(this);
		mSemC = new SchemaTextController(this);
		mSyntaxC = new XSDTextController(this);
		

		// Los otros no son inicializados
	}

	public EditorsUI getView() {
		return mView;
	}
	
	@Override
	public void setView(EditorsUI mView) {
		this.mView = mView;
		this.mView.setController(this);
		this.assingViewsToControllers();
	}
	
	private void assingViewsToControllers(){
		mTextC.setView(mView.getTextMode());
		mSemC.setView(mView.getSemanticMode());
		mSyntaxC.setView(mView.getSyntaxMode());
		// Asginar los otros controladores cuando esten listos
	}
	
	@Override
	public IMainController getMainController() {
		return mMainController.getMainController();
	}
	
	@Override
	public Model getModel() {
		return getMainController().getModel();
	}
	
	/**
	 * 
	 * @return the type of the selected file text editor
	 */
	@Override
	public EnumModelType getFocusedTab(){
		EnumModelType res = null;
		Component selected = mView.getSelectedComponent();
		if(selected instanceof EditorTextMode){
			res = EnumModelType.XML;
		}else if (selected instanceof EditorGraphMode){			
			res = EnumModelType.GEM;
		}else if (selected instanceof EditorSyntaxMode){
			res = EnumModelType.XSD;
		}else if (selected instanceof EditorSemanticMode){
			res = EnumModelType.SCH;
		}else if (selected instanceof EditorGraphMode){
			res = EnumModelType.GEM;
		}else{
			res = null;
		}
		return res;		
	}
	
	@Override
	public void loadText(String s, EnumModelType type) {
		AbstractTextEditorController c;
		switch(type){
		case XML:
			c = mTextC;
			break;
		case XSD:
			c= mSyntaxC;
			break;
		case SCH:
			c = mSemC;
			break;	
		default:
			c =null;
		}
		c.setText(s);	
	}
	
	@Override
	public String getText(EnumModelType type) {
		AbstractTextEditorController c;
		switch(type){
		case XML:
			c = mTextC;
			break;
		case XSD:
			c= mSyntaxC;
			break;
		case SCH:
			c = mSemC;
			break;	
		default:
			c =null;
		}
		return c.getTextFromView();
	}
	
	@Override
	public AbstractTextEditorController getController(EnumModelType type) {
		switch(type){
		case XML:
			return getXMLTextController();
		case XSD:
			return getXSDTextController();
		case SCH:
			return getSchemaTextController();
		}
		return null;
	}
	@Override
	public XMLTextController getXMLTextController() {
		return mTextC;
	}
	@Override
	public XSDTextController getXSDTextController() {
		return mSyntaxC;
	}
	@Override
	public SchemaTextController getSchemaTextController() {
		return mSemC;
	}

	@Override
	public AbstractTextEditorController getControllerOfFocusedTab() {
		return getController(getFocusedTab());
	}
	
	public void loadGraphMode(EnumModelType currentTab){
		this.getMainController().graphParse(currentTab);
	}
	
	public void updateTextMode(EnumModelType currentTab){
		if (MainGraphEdition.hasContentChanged()){
			this.mTextC.setText(getModel().get(EnumModelType.XML).getDomModel().getTextVersion());
			MainGraphEdition.unSetContentChanged();
		}
	}
	
	
}
